﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;

namespace RandomDemo.TokenFiles
{
    /// <summary>
    /// 保存一些Token用到的单例静态的数据
    /// </summary>
    public class SingTon
    {
        private static SingTon instence;
        private static object o = new object();
        private static List<JwtSecurityToken> jwts = new List<JwtSecurityToken>();
        private SingTon()
        {
            GC();
        }
        /// <summary>
        /// 清除过期token
        /// </summary>
        private static async void GC()
        {
            while (true)
            {
                var temp = jwts.Where(m => m.ValidTo < DateTime.UtcNow);
                foreach (var token in temp)
                {
                    jwts.Remove(token);
                }
                await Task.Delay(new TimeSpan(0, 30, 0));
            }
        }
        public static SingTon Instence
        {
            get
            {
                if (instence == null)
                {
                    lock (o)
                    {
                        if (instence == null)
                        {
                            instence = new SingTon();
                        }
                    }
                }
                return instence;
            }
        }
        public List<JwtSecurityToken> Jwts
        {
            get { return jwts; }
        }
    }
}
